﻿var currentPage = 1; // 当前页
var pageSize = 6; // 每页显示多少条
var pageall = 0; // 总页数
var total = 0; // 总记录数

$(document).ready(function() {
    var id = LexunJS.GetQueryString("id"); // 客源ID
    // 修改
    if (id > 0) {
        $("#hID").val(id);
        //ShowFollow(1);
        showDetail(id);
    }
    document.getElementById("customerId").value = id;
    document.getElementById("followType").value = "电话沟通"; //跟进方式默认选择“电话沟通”


    // 默认标签
    var tagNo = LexunJS.GetQueryString("tagNo");
    if (parseInt(tagNo) <= 0 || tagNo == "") tagNo = 2;
    tagNo = parseInt(tagNo);

    go_to(tagNo);
});

// 查询详细
function showDetail(id) {
    $.ajax({
        type: 'POST',
        url: '../Handler/HandlerCustomer.ashx',
        data: 'act=customer_detail&id=' + id,
        timeout: '10000',
        error: function() {
            alert("修改失败！");
        },
        success: function(json) {
            showDetailCallBack(json);
        }
    });
}

function showDetailCallBack(response) {
    var json = eval("(" + response + ")");
    var record = json.record;

    if (record == null || record.length == 0)
        return;

    var sessionUserID = $("#hUserID").val();        // 当前登录用户
    var isManager = $("#hIsManager").val();         // 当前登录用户 0不是 1是管理层
    var isManager1 = $("#hIsManager1").val();       // 是否是最原始客源的管理者  0不是 1是管理层
    var hChangeUserId = $("#hChangeUserId").val();
    var hChangeStatus=$("#hChangeStatus").val();
    var ddeptid=$("#ddeptid").val();
    var recordLength = record.length;
    for (var i = 0; i < recordLength; i++) {
        $("#sCustomerID").text(record[i].id);
        $("#sCustomer").text(record[i].customer);

        if (record[i].mark1 == 1) $("#sOutmsg").html("<font color='red'>【温馨提示：待审核客源,不允许转客。】</font>");

        // 判断是否有权限查看
        if (record[i].ownerid == sessionUserID || (record[i].isself == 1 && record[i].mark1 != 1) || isManager == 1 || (record[i].ischange==1 && record[i].adduid==hChangeUserId) ||( record[i].ischange==1 && isManager1==1)) {
            $("#sCustomerTel").text(record[i].customertel);
            $("#sAgent").text(record[i].agent);
            $("#sAgentTel").text(record[i].agenttel);
        }
        else {
            $("#sCustomerTel").text("不是您的客源,请联系所属人");
            $("#sAgent").text("不是您的客源,请联系所属人");
            $("#sAgentTel").text("不是您的客源,请联系所属人");
        }

        if (record[i].ownerid == sessionUserID || (record[i].isself == 1 && record[i].mark1 != 1) || isManager == 1) {
            $("#sCustomerTel").text(record[i].customertel);
            $("#sAgent").text(record[i].agent);
            $("#sAgentTel").text(record[i].agenttel);
        }
        
        //接介客,原客所属人可以看到电话
        if (record[i].ischange == '2' && (record[i].adduid == sessionUserID  || isManager1 == 1)) {
            $("#sCustomerTel").text(record[i].customertel);
            $("#sAgent").text(record[i].agent);
            $("#sAgentTel").text(record[i].agenttel);
        }

        // 转介过程中, 所属人不是自己 且为非管理者 且登录者也不是最原始的管理者
        if (record[i].ischange == 1 && isManager1 == 0) {//转介方经理是否有权限
            if (record[i].ischange == 1 && record[i].adduid != hChangeUserId) {//判断是否是转介人 
                if ((record[i].ownerid != sessionUserID && hChangeStatus != 0) || (record[i].ischange == '1' && isManager != 1)) {//接受人，经理是否有权限
                    $("#sCustomerTel").text("接收后方可看电话号码");
                    $("#sAgent").text("接收后方可看电话号码");
                    $("#sAgentTel").text("接收后方可看电话号码");
                }
            } 
        }
        $("#sOccupation").text(record[i].occupation);

        if (record[i].status == 0)
            $("#sStatus").text("当前");
        else if (record[i].status == 1)
            $("#sStatus").text("资料");

        $("#sTradetype").text(record[i].tradetype);
        $("#sUsage").text(LexunJS.GetUsageNameById(record[i].usage));
        $("#sRemark").text(record[i].remark);
        $("#sCustomerClass").text(record[i].customerclass);
        $("#sInfoFrom").text(record[i].infofrom);
        $("#sEstates").text(record[i].estates);
        $("#sDname").text(record[i].dname);
        $("#sArea").text(record[i].area);
        $("#sPayType").text(record[i].paytype);
        $("#sType").text(record[i].type);
        $("#sDirect").text(record[i].direct);

        if (record[i].isself == 0)
            $("#sIsSelf").text("私客");
        else if (record[i].isself == 1)
            $("#sIsSelf").text("公客");

        $("#sFloor").text(record[i].minfloor + " ~ " + record[i].maxfloor);
        $("#sHouseStyle").text(record[i].housestyle);
        $("#sFitment").text(record[i].fitment);
        $("#sRoom").text(record[i].room1 + "~" + record[i].room + "房" + record[i].hall + "厅" + record[i].restroom + "卫" + record[i].balcony + "阳台");
        $("#sAcreage").text(record[i].minacreage + " ~ " + record[i].maxacreage);
        if (record[i].tradetype == "求租") {
            $("#sPrice").text(record[i].minprice + " ~ " + record[i].maxprice + " 元/月");
        } else {
            $("#sPrice").text(record[i].minprice + " ~ " + record[i].maxprice + " 万");
        }

        $("#sAddUser").text(record[i].adduser);
        $("#sAddDept").text(record[i].adddept);
        $("#sAddDate").text(record[i].adddate);
        $("#sOwner").text(record[i].owner);
        $("#sOwnerDept").text(record[i].ownerdept);
        $("#sOwnerDate").text(record[i].ownerdate);
        $("#sOwnerTel").text(record[i].ownertel);
        $("#sModifyUser").text(record[i].modifyuser);
        $("#sModifyDate").text(record[i].modifydate);
        $("#sBuyingMotive").text(record[i].buyingmotive);
        
        // div3中显示的信息
        $("#div3_sArea").text(record[i].area);
        $("#div3_sDname").text(record[i].dname);        
        $("#div3_sUse").text(LexunJS.GetUsageNameById(record[i].usage));
        $("#div3_sBuildName").text(record[i].estates);      // 意向楼盘
        $("#div3_sTradetype").text(record[i].tradetype);    // 交易类型
        $("#div3_sMinTotalPrice").text(record[i].minprice); // 最低价格
        $("#div3_sMaxTotalPrice").text(record[i].maxprice); // 最高价格
        $("#mark3").text(record[i].mark3); //转介备注;
        var mark2 = record[i].mark2; //看房次数
        if (mark2 != "") {
            $("#mark2").text(mark2 + "次看房");
        }
        
        
        if (record[i].tradetype == "求租") {
            $("#div3_unit").text("元");
        } else {
            $("#div3_unit").text("万");
        }

        $("#div3_sMinAcreage").text(record[i].minacreage);
        $("#div3_sMaxAcreage").text(record[i].maxacreage);
        $("#div3_sRoomHall").text(record[i].room);
        $("#div3_sDirect").text(record[i].direct);
        $("#div3_sFitment").text(record[i].fitment);
    }
}

// 跳转 num(跳到第几个标签)
function go_to(num) {
    var tabs = document.getElementById("div_tab").getElementsByTagName("a");

    for (var i = 1; i <= tabs.length; i++) {
        if (num == i) {
            tabs[i - 1].className = "y";
            document.getElementById("div" + i).style.display = "block";
        } else {
            tabs[i - 1].className = "";
            document.getElementById("div" + i).style.display = "none";
        }
    }

    var id = $("#hID").val();
    switch (num) {
        case 1:
            showDetail(id); // 详细信息
            break;
        case 2:
            ShowFollow(1);  // 查询更进
            break;
        case 3:
            showHousing(1); // 匹配房源
            break;
        case 4:
            ShowAlert(1);   // 提醒信息
            break;
        case 5:
            ShowChangeCustomer(1);   // 转介客记录
            break;
        case 6:
            document.getElementById("tag6").style.display = "block";
            ShowTranctionDetail();   // 成交信息
            break;
        case 7:
            showOtherCustomer();
            break;    
    }
}

//默认查询跟进
function ShowFollow(currentPage) {
    var id = LexunJS.GetQueryString("id");
    $.ajax({
        type: 'POST',
        url: '../Handler/HandlerCustomer.ashx',
        data: 'act=SelectCustomerFollow&id=' + id + '&page=' + currentPage + '&pagesize=' + pageSize,
        timeout: '10000',
        error: function() {
            alert("修改失败！");
        },
        success: function(json) {
            ShowFlowCallBack(json);
        }
    });
}

//点击刷新,查询跟进
function ShowFollow1(currentPage) {
    var id = LexunJS.GetQueryString("id");
    $.ajax({
        type: 'POST',
        url: '../Handler/HandlerCustomer.ashx',
        data: 'act=SelectCustomerFollow&id=' + id + '&page=' + currentPage + '&pagesize=' + pageSize + '&refresh=1',
        timeout: '10000',
        error: function() {
            alert("修改失败！");
        },
        success: function(json) {
            ShowFlowCallBack(json);
        }
    });
}

//查询更多跟进
function ShowFollow2(currentPage) {
    var id = LexunJS.GetQueryString("id");
    $.ajax({
        type: 'POST',
        url: '../Handler/HandlerCustomer.ashx',
        data: 'act=SelectCustomerFollow&id=' + id + '&page=' + currentPage + '&pagesize=1000&refresh=1',
        timeout: '10000',
        error: function() {
            alert("修改失败！");
        },
        success: function(json) {
            ShowFlowCallBack(json);
        }
    });
}

function ShowFlowCallBack(response) {
    var json = eval("(" + response + ")");
    var record = json.record;

    var page = json.page;

    currentPage = page.currentpage;
    pageall = parseInt(page.pageall);
    total = page.total;

    $("#divPage").html(""); // 清空

    $("#divPage").html("<a href=\"javascript:ShowFollow1(1)\">刷新</a><a href=\"javascript:ShowFollow2(1)\">更多...</a>");

    if (record == null || record.length == 0) return;
    $("#divfollow").html(""); // 显示列表
    var arr = [];
    arr.push("<table id='table1' style=\"word-break: break-all;\" width='100%' border='0' cellpadding='0' cellspacing='1' bgcolor='#a8c7ce'>");
    arr.push("<tr>");
    arr.push("<td bgcolor='d3eaef' class='STYLE10'>");
    arr.push("序号");
    arr.push("</td>");
    arr.push("<td width='110' bgcolor='d3eaef' class='STYLE10'>");
    arr.push("跟进时间</td>");
    arr.push("<td width='80' bgcolor='d3eaef' class='STYLE10'>");
    arr.push("部门</td>");
    arr.push("<td width='50' bgcolor='d3eaef' class='STYLE10'>");
    arr.push("跟进人</td>");
    arr.push("<td width='50' bgcolor='d3eaef' class='STYLE10'>");
    arr.push("跟进方式</td>");
    arr.push("<td bgcolor='d3eaef' class='STYLE10'>");
    arr.push("跟进内容</td>");
    arr.push("<td bgcolor='d3eaef' class='STYLE10'>");
    arr.push("所看房源编号</td>");

    arr.push("</tr>");
    for (var i = 0; i < record.length; i++) {
        if (i % 2 == 0)
            arr.push("<tr id='tr_" + record[i].id + "' bgcolor='#FFFFFF' style='cursor: pointer;' >");
        else
            arr.push("<tr id='tr_" + record[i].id + "' bgcolor='#EEEDED' style='cursor: pointer;' >");

        arr.push("<td class='STYLE6'>");
        arr.push(record[i].id);
        arr.push("</td>");
        arr.push("<td class='STYLE6'>");
        arr.push(record[i].fdate);
        arr.push("</td>");
        arr.push("<td class='STYLE6'>");
        arr.push(record[i].fdept);
        arr.push("</td>");
        arr.push("<td class='STYLE6'>");
        arr.push(record[i].fuser);
        arr.push("</td>");
        arr.push("<td class='STYLE6'>");
        arr.push(record[i].ftype);
        arr.push("</td>");
        arr.push("<td align='left' class='STYLE7'>");
        arr.push(record[i].fcontent);
        arr.push("</td>");
        arr.push("<td class='STYLE6'>");
        if (record[i].fhousingid == 0) {
            arr.push("<font color='blue'>无看房记录</font></td>")
        } else {
            arr.push("<a href='javascript:ShowDialogs(\"HousingDetail.aspx\",\"" + record[i].fhousingid + "\");'><font color='red'>" + record[i].fhousingid + "</font></a></td>");
        }

        arr.push("</tr>");
    }
    arr.push("</table>");
    $("#divfollow").html(arr.join('')); // 显示列表
}

// 匹配出的房源
function showHousing(currentPage) {
    var id = LexunJS.GetQueryString("id"); // 楼盘ID
    if (id <= 0) {
        alert("请先选择您要操作的行！");
        return;
    }

    var dname = ""; // 城区
    var area = "";  // 片区
    if (document.getElementById("cbDname").checked) {
        dname = $("#div3_sDname").text();
        area = $("#div3_sArea").text();
    }

    var usage = ""; // 用途
    if (document.getElementById("cbUse").checked){
        usage = $("#div3_sUse").text();
        if(usage=="住宅")
                usage = "1,4";
        else if(usage=="写字楼")
                usage = "3,4";
        else if(usage=="商铺")
                usage = "2";
        else if(usage=="厂房")        
                usage = "6";
        else if(usage=="民房")        
                usage = "10";
        
    }
        

    var buildname = ""; // 意向楼盘
    if (document.getElementById("cbBuildName").checked)
        buildname = $("#div3_sBuildName").text();

    var tradetype = "";
    if (document.getElementById("cbTradetype").checked)
        tradetype = $("#div3_sTradetype").text();

    var minPrice = 0;
    var maxPrice = 0
    if (document.getElementById("cbTotalPrice").checked) {
        minPrice = $("#div3_sMinTotalPrice").text();
        maxPrice = $("#div3_sMaxTotalPrice").text();
    }

    var minAcreage = 0;
    var maxAcreage = 0
    if (document.getElementById("cbAcreage").checked) {
        minAcreage = $("#div3_sMinAcreage").text();
        maxAcreage = $("#div3_sMaxAcreage").text();
    }

    var room = "";      // 房
    if (document.getElementById("cbRoomHall").checked)
        room = $("#div3_sRoomHall").text();

    var direct = "";    // 朝向
    if (document.getElementById("cbDirect").checked)
        direct = $("#div3_sDirect").text();

    var fitment = "";   // 装修
    if (document.getElementById("cbFitment").checked)
        fitment = $("#div3_sFitment").text();

    $.ajax({
        type: 'POST',
        url: '../Handler/HandlerHousing.ashx',
        data: 'act=housing_match&page=' + currentPage + '&pagesize=' + pageSize + '&dname=' + dname + '&area=' + area + '&usage=' + usage
                 + '&buildname=' + buildname + '&tradetype=' + tradetype + '&minPrice=' + minPrice + '&maxPrice=' + maxPrice
                 + '&minAcreage=' + minAcreage + '&maxAcreage=' + maxAcreage + '&room=' + room + '&direct=' + direct ,
        timeout: '10000',
        error: function() {
            alert("匹配失败！");
        },
        success: function(json) {
            showHousingCallBack(json);
        }
    });
}

function showHousingCallBack(response) {
    var json = eval("(" + response + ")");
    var record = json.record;

    var page = json.page;

    currentPage = page.currentpage;
    pageall = parseInt(page.pageall);
    total = page.total;
    if (record == null || record.length == 0) {
        $("#div3_page").html(""); // 清空
        $("#div3_list").html("<strong><font color='red'>未匹配到合适的房源</font></strong>");
        return;
    }

    var arr = [];

    arr.push("<table id='table1' width='100%' border='0' cellpadding='0' cellspacing='1' bgcolor='#a8c7ce'>");
    arr.push("<tr>");
    arr.push("<td bgcolor='d3eaef' class='STYLE10'>");
    arr.push("编号</td>");
    arr.push("<td bgcolor='d3eaef' class='STYLE10'>");
    arr.push("楼盘名称</td>");
    arr.push("<td bgcolor='d3eaef' class='STYLE10'>");
    arr.push("栋阁名称</td>");
    arr.push("<td bgcolor='d3eaef' class='STYLE10'>");
    arr.push("楼层</td>");
    arr.push("<td bgcolor='d3eaef' class='STYLE10'>");
    arr.push("房间号</td>");
    arr.push("<td bgcolor='d3eaef' class='STYLE10'>");
    arr.push("城区</td>");
    arr.push("<td bgcolor='d3eaef' class='STYLE10'>");
    arr.push("片区</td>");
    arr.push("<td bgcolor='d3eaef' class='STYLE10'>");
    arr.push("面积</td>");
    arr.push("<td bgcolor='d3eaef' class='STYLE10'>");
    arr.push("总价</td>");
    arr.push("<td bgcolor='d3eaef' class='STYLE10'>");
    arr.push("户型</td>");
    arr.push("</tr>");
    for (var i = 0; i < record.length; i++) {
        if (i % 2 == 0)
            arr.push("<tr id='tr_" + record[i].id + "' bgcolor='#FFFFFF' >");
        else
            arr.push("<tr id='tr_" + record[i].id + "' bgcolor='#EEEDED' >");

        arr.push("<td class='STYLE6'>");
        //arr.push("" + record[i].id + "</td>");
        arr.push("<a href='javascript:ShowDialogs(\"HousingDetail.aspx\",\"" + record[i].id + "\");'><font color='red'>" + record[i].id + "</font></a></td>");
        arr.push("<td class='STYLE6'>");
        arr.push("" + record[i].ename + "</td>");
        arr.push("<td class='STYLE6'>");
        arr.push("" + record[i].buildname + "</td>");
        arr.push("<td class='STYLE6'>");
        arr.push("" + record[i].mark1 + "</td>");
        arr.push("<td class='STYLE6'>");
        arr.push("" + record[i].roomno + "</td>");
        arr.push("<td class='STYLE6'>");
        arr.push("" + record[i].dname + "</td>");
        arr.push("<td class='STYLE6'>");
        arr.push("" + record[i].area + "</td>");
        arr.push("<td class='STYLE6'>");
        arr.push("" + record[i].acreage + "</td>");
        arr.push("<td class='STYLE6'>");
        arr.push("" + record[i].totalprice + "</td>");
        arr.push("<td class='STYLE6'>");
        arr.push("" + record[i].room + "房" + record[i].hall + "厅</td>");
        arr.push("</tr>");
    }
    arr.push("</table>");
    $("#div3_list").html(arr.join('')); // 显示列表
    $("#div3_page").html(""); // 清空

    if (pageall > 1) {
        var pageHtml = LexunJS.showPage2(currentPage, pageall, "showHousing"); // 分页
        $("#div3_page").html(pageHtml);
    }
}

// 弹出窗口,查看某匹配出的房源详细
function showHousingDialog(id) {
    var path = "../PM/HousingDetail.aspx?id=" + id;
    var parameter = "房源详情";

    var id_list = $("#hID_List").val();
    var url = "../PM/HousingDetail.aspx?id=" + id + "&id_list=" + escape(id_list);

    var style = "left = 50, screenX = 0, top = 50, screenY = 0, resizable = yes, scrollbars = yes,width=750px, height=600px,Toolbar=no,Location=no,Direction=no,Resizeable=no";
    window.open(url, parameter, style);

    //window.open(path, parameter, style);
}

//查询提醒信息
function ShowAlert(currentPage) {
    var id = LexunJS.GetQueryString("id");
    $.ajax({
        type: 'POST',
        url: '../Handler/HandlerCustomer.ashx',
        data: 'act=SelectCustomerAlert&id=' + id + '&page=' + currentPage + '&pagesize=' + pageSize,
        timeout: '10000',
        error: function() {
            alert("修改失败！");
        },
        success: function(json) {
            ShowAlertCallBack(json);
        }
    });
}

function ShowAlertCallBack(response) {
    var json = eval("(" + response + ")");
    var record = json.record;

    var page = json.page;

    currentPage = page.currentpage;
    pageall = parseInt(page.pageall);
    total = page.total;
    if (record == null || record.length == 0) return;
    $("#divAlert").html("");
    var arr = [];
    arr.push("<table id='table1' width='100%' border='0' cellpadding='0' cellspacing='1' bgcolor='#a8c7ce'>");
    arr.push("<tr>");
    arr.push("<td bgcolor='d3eaef' class='STYLE10'>");
    arr.push("<input type=\"checkbox\" name=\"checkboxAll\" id=\"checkboxAll\" onclick=\"cbSelectAll()\" />");
    arr.push("</td>");
    arr.push("<td bgcolor='d3eaef' class='STYLE10'>");
    arr.push("人员</td>");
    arr.push("<td bgcolor='d3eaef' class='STYLE10'>");
    arr.push("部门</td>");
    arr.push("<td bgcolor='d3eaef' class='STYLE10'>");
    arr.push("提醒时间</td>");
    arr.push("<td bgcolor='d3eaef' class='STYLE10'>");
    arr.push("提醒类型</td>");
    arr.push("<td bgcolor='d3eaef' class='STYLE10'>");
    arr.push("提醒内容</td>");
    arr.push("<td bgcolor='d3eaef' class='STYLE10'>");
    arr.push("处理标志</td>");
    arr.push("<td bgcolor='d3eaef' class='STYLE10'>");
    arr.push("处理时间</td>");
    arr.push("<td bgcolor='d3eaef' class='STYLE10'>");
    arr.push("处理人</td>");
    arr.push("<td bgcolor='d3eaef' class='STYLE10'>");
    arr.push("处理部门</td>");

    arr.push("</tr>");
    for (var i = 0; i < record.length; i++) {
        arr.push("<tr>");
        arr.push("<td bgcolor='d3eaef' class='STYLE10'>");
        arr.push("<input type='checkbox' name='checkbox_" + record[i].id + "' id='" + record[i].id + "' onclick=\"cbSelect(this, " + record[i].id + ")\" />");
        arr.push("</td>");
        arr.push("<td bgcolor='d3eaef' class='STYLE10'>");
        arr.push("" + record[i].adduser + "</td>");
        arr.push("<td bgcolor='d3eaef' class='STYLE10'>");
        arr.push("" + record[i].adddept + "</td>");
        arr.push("<td bgcolor='d3eaef' class='STYLE10'>");
        arr.push("" + record[i].alertdate + "</td>");
        arr.push("<td bgcolor='d3eaef' class='STYLE10'>");
        arr.push("" + record[i].alerttype + "</td>");
        arr.push("<td bgcolor='d3eaef' class='STYLE7'>");
        arr.push("" + record[i].alertcontent + "</td>");
        if (record[i].dflag == 1) {
            arr.push("<td bgcolor='d3eaef' class='STYLE10'>");
            arr.push("<font color='red'>已处理</font></td>");
        } else {
            arr.push("<td bgcolor='d3eaef' class='STYLE10'>");
            arr.push("<font color='red'>未处理</font></td>");
        }

        arr.push("<td bgcolor='d3eaef' class='STYLE10'>");
        if (record[i].dflag == 1) {
            arr.push("" + record[i].ddate + "</td>");
        } else {
            arr.push("</td>");
        }
        arr.push("<td bgcolor='d3eaef' class='STYLE10'>");
        arr.push("" + record[i].duser + "</td>");
        arr.push("<td bgcolor='d3eaef' class='STYLE10'>");
        arr.push("" + record[i].ddept + "</td>");
        arr.push("</tr>");
    }
    arr.push("</table>");
    $("#divAlert").html(arr.join('')); // 显示列表
    $("#divAlertPage").html(""); // 清空
    if (pageall > 1) {
        var pageHtml = LexunJS.showPage2(currentPage, pageall, "ShowAlert"); // 分页
        $("#divAlertPage").html(pageHtml);
    }
}

// 转介客记录
function ShowChangeCustomer(currentPage) {
    var id = LexunJS.GetQueryString("id");
    $.ajax({
        type: 'POST',
        url: '../Handler/HandlerCustomer.ashx',
        data: 'act=changeCustomer_list&id=' + id + '&page=' + currentPage + '&pagesize=' + pageSize,
        timeout: '10000',
        error: function() {
            alert("修改失败！");
        },
        success: function(json) {
            ShowChangeCustomerCallBack(json);
        }
    });
}

function ShowChangeCustomerCallBack(response) {
    var json = eval("(" + response + ")");
    var record = json.record;

    if (record == null || record.length == 0) {
        $("#divChangeCustomer").html("<font color='red'><strong>暂无转介记录!</strong></font>");
        $("#divChangeCustomerPage").html("");
        return;
    }

    var page = json.page;

    currentPage = page.currentpage;
    pageall = parseInt(page.pageall);
    total = page.total;

    var arr = [];
    arr.push("<table id='table1' width='100%' border='0' cellpadding='0' cellspacing='1' bgcolor='#a8c7ce'>");
    arr.push("<tr>");
    arr.push("<td bgcolor='d3eaef' class='STYLE10'>");
    arr.push("<input type=\"checkbox\" name=\"checkboxAll\" id=\"checkboxAll\" onclick=\"cbSelectAll()\" />");
    arr.push("</td>");
    arr.push("<td bgcolor='d3eaef' class='STYLE10'>");
    arr.push("转介人</td>");
    arr.push("<td bgcolor='d3eaef' class='STYLE10'>");
    arr.push("转介人部门</td>");
    arr.push("<td bgcolor='d3eaef' class='STYLE10'>");
    arr.push("转介时间</td>");
    arr.push("<td bgcolor='d3eaef' class='STYLE10'>");
    arr.push("接收人</td>");
    arr.push("<td bgcolor='d3eaef' class='STYLE10'>");
    arr.push("接收人部门</td>");
    arr.push("<td bgcolor='d3eaef' class='STYLE10'>");
    arr.push("接收时间</td>");
    arr.push("<td bgcolor='d3eaef' class='STYLE10'>");
    arr.push("状态</td>");

    arr.push("</tr>");
    for (var i = 0; i < record.length; i++) {
        arr.push("<tr>");
        arr.push("<td bgcolor='d3eaef' class='STYLE10'>");
        arr.push("<input type='checkbox' name='checkbox_" + record[i].id + "' id='" + record[i].id + "' onclick=\"cbSelect(this, " + record[i].id + ")\" />");
        arr.push("</td>");
        arr.push("<td bgcolor='d3eaef' class='STYLE10'>");
        arr.push("" + record[i].changeuser + "</td>");
        arr.push("<td bgcolor='d3eaef' class='STYLE10'>");
        arr.push("" + record[i].changedept + "</td>");
        arr.push("<td bgcolor='d3eaef' class='STYLE10'>");
        arr.push("" + record[i].changedate + "</td>");
        arr.push("<td bgcolor='d3eaef' class='STYLE10'>");
        arr.push("" + record[i].acceptuser + "</td>");
        arr.push("<td bgcolor='d3eaef' class='STYLE10'>");
        arr.push("" + record[i].acceptdept + "</td>");
        arr.push("<td bgcolor='d3eaef' class='STYLE10'>");
        arr.push("" + record[i].accpetdate + "</td>");

        arr.push("<td bgcolor='d3eaef' class='STYLE10'>");
        if (record[i].status == 0)
            arr.push("<font color='red'>转介成功</font>");
        if (record[i].status == 1)
            arr.push("<font color='red'>待接收人确认</font>");
        else if (record[i].status == 2)
            arr.push("<font color='red'>转介失败</font>");
        arr.push("<td>");
    }
    arr.push("</table>");

    $("#divChangeCustomer").html(arr.join('')); // 显示列表
    $("#divChangeCustomerPage").html(""); // 清空
    if (pageall > 1) {
        var pageHtml = LexunJS.showPage2(currentPage, pageall, "ShowChangeCustomer"); // 分页
        $("#divChangeCustomerPage").html(pageHtml);
    }
}

// 成交信息
function ShowTranctionDetail() {
    var id = $("#hID").val();

    if (id <= 0)
        return;

    $.ajax({
        type: 'POST',
        url: '../Handler/HandlerHousing.ashx',
        data: 'act=housingTranction_detail&kty_id=' + id,
        timeout: '10000',
        error: function() {
            alert("系统繁忙，请稍候后试！");
        },
        success: function(json) {
            ShowTranctionDetailCallBack(json);
        }
    });
}

function ShowTranctionDetailCallBack(response) {
    var json = eval("(" + response + ")");
    var record = json.record;

    if (record == null || record.length == 0) return;

    var recordLength = record.length;
    for (var i = 0; i < recordLength; i++) {
        $("#sPfyx").text(record[i].kfyx + record[i].kfqy + record[i].kffh);
        $("#sDdate").text(record[i].ddate);
        $("#sMname").text(record[i].mname);
        $("#sDtype").text(record[i].dtype);
        $("#sBuyuse").text(record[i].buyuse);
        $("#sPaystate").text(record[i].paystate);
        $("#sKty_id").text(record[i].kty_id);
        $("#sKname").text(record[i].kname);
    }
}

// 单选
function cbSelect(obj, objvalue) {
    if (obj.checked) {
        document.getElementById("fid").value = "";
        LexunJS.addOrCutData(1, "fid", objvalue);
    } else {
        LexunJS.addOrCutData(0, "fid", objvalue);
    }
}

// 全选
function cbSelectAll() {
    document.getElementById("fid").value = "";
    var cbAll = document.getElementById("checkboxAll");
    var checkList = document.getElementsByTagName("input");

    if (cbAll.checked) {
        for (var i = 0; i < checkList.length; i++) {
            if (checkList[i].type == "checkbox") {
                checkList[i].checked = true;
                if (parseInt(checkList[i].id) > 0) LexunJS.addOrCutData(1, "fid", checkList[i].id);
            }
        }
    } else {
        for (var i = 0; i < checkList.length; i++) {
            if (checkList[i].type == "checkbox") {
                checkList[i].checked = false;
                if (parseInt(checkList[i].id) > 0) LexunJS.addOrCutData(0, "fid", checkList[i].id);
            }
        }
    }
}
//弹出窗口
function ShowDialog(url) {
    var id = LexunJS.GetQueryString("id"); // 楼盘ID
    if (id == "") {
        alert("请选择需要跟进的客源！");
        return;
    }
    var path = url + "?id=" + id;
    var parameter = "客源跟进";
    var style = "dialogWidth:570px; dialogHeight:430px; status:0;scroll:1; help:0; resizable:1;Scrollbars=yes,Toolbar=no,Location=no,Direction=no,Resizeable=no";
    window.showModalDialog(path, parameter, style);
    go_to(2);

}
function ShowDialogs(url, id) {
    var url = "../PM/"+url + "?id=" + id;

    //    self.parent.closeTab('房源详细');
    //    self.parent.addTab('房源详细', url, 'icon-nav');
    var style = "left = 80, screenX = 0, top = 100, screenY = 0, resizable = yes, scrollbars = yes,width=750px, height=600px,Toolbar=no,Location=no,Direction=no,Resizeable=no";
    
    window.open(url, "带看房", style);
    
   
}

//处理提醒
function UpdateAlert() {
    var IDList = document.getElementById("fid").value;
    if (IDList == "") {
        alert("请先选择您要处理的行！");
        return;
    }
    // ajax修改
    $.ajax({
        type: 'POST',
        url: '../Handler/HandlerCustomer.ashx',
        data: 'act=p_CustomerAlert_Update&id=' + IDList,
        timeout: '10000',
        error: function() {
            alert("处理失败！");
        },
        success: function(outmsg) {
            if (outmsg < 1) {
                alert("处理失败！");
            }
            ShowAlert(currentPage);

        }
    });
}

// 删除
function del() {
    var IDList = document.getElementById("hID").value;
    if (IDList == "") {
        alert("请先选择您要删除的行！");
        return false;
    }

    if (confirm('确认删除该客源，请谨慎操作！')) {
            // ajax删除
        $.ajax({
            type: 'POST',
            url: '../Handler/HandlerCustomer.ashx',
            data: 'act=customer_del&id=' + IDList,
            timeout: '10000',
            error: function() {
                alert("删除失败！");
            }, success: function(outmsg) {
                $("#hID").val("");
                alert(outmsg);
                window.location.reload();
                return true;
            }
        });
       }
   
    return false;
}

// 转私客 转公客 isself 0私客 1公客
function change(isself) {
    var IDList = document.getElementById("hID").value;
    if (IDList == "") {
        alert("请先选择您要操作的行！");
        return false;
    }
    var ManagerIsSelf = document.getElementById("hManagerChangIsSelf").value;
    if (ManagerIsSelf == "1" && isself == "0") {//经理级转私客指定接客人

        obj = document.getElementById('dvToPty')
        if (obj.style.display == 'block') {
            obj.style.display = 'none';
        } else {

            $("#tbMakeUser").autocomplete("../Ajax/GetUserAuto.aspx", { minChars: 2, extraParams: { exists: '1', uname: function() { return escape($('#tbMakeUser').val()); } }, scrollHeight: 350, width: 230, max: 100 });
            obj.style.display = 'block';
            $("#tbMakeUser").focus();
        }
    } else {
            var msg = "";
            if (isself == "0") {
                msg = "确认转私客吗？";
            } else {
                msg = "确认转公客吗？";
             }
        if (confirm(msg)) {
            $.ajax({
                type: 'POST',
                url: '../Handler/HandlerCustomer.ashx',
                data: 'act=customer_change&id=' + IDList + "&isself=" + isself,
                timeout: '10000',
                error: function() {
                    alert("操作失败！");
                }, success: function(outmsg) {
                     alert(outmsg);
                    if(outmsg=="转公客成功!" ||outmsg=="申请转私客成功!"){
                         window.location.reload();
                         return true;
                    }

                }
            });
        }

    }

    return false;
}
function chkToPty() {
    var objValue = document.getElementById('tbMakeUser').value;

    if (objValue == "") {
        alert("开盘人不能为空!");
        return false;
    }
}

// 转介客
function changeTo() {
    var IDList = document.getElementById("hID").value;
    if (IDList == "") {
        alert("请先选择您要操作的行！");
        return false;
    }

    var path = "CustomerChange.aspx?idlist=" + IDList;
    var parameter = "确认接收人";
    var style = "dialogWidth:450px; dialogHeight:260px; status:1;scroll:1; help:1; resizable:0;Scrollbars=yes,Toolbar=no,Location=yes,Direction=yes,Resizeable=yes";
    window.showModalDialog(path, parameter, style);
    window.location.reload();

    return false;
}

// 打开高级查询框
function iAdvancedSearch_onclick() {
    var obj = document.getElementById("divAdvancedSearch");
    $("#divAdvancedSearch").toggle();
}


//转成交盘
function CustomerToDeal(obj) {
    if (obj.value == '转成交') {
        if (confirm('确认转成交客!')) {
            window.location.reload();
            return true;
        }
        else {
            return false
        }
        return false;
    } else {
        if (confirm('确认取消成交客!')) {
            window.location.reload();
            return true;
        }
        else {
            return false
        }
        return false;
    }
}
//转资料

function CustomerToDatum(obj) {
    if (obj.value == '转资料') {
        if (confirm('确认转资料!')) {
            window.location.reload();
            return true;
        }
        else {
            return false
        }
        return false;
    } else {
        if (confirm('确认转当前!')) {
            window.location.reload();
            return true;
        }
        else {
            return false
        }
        return false;
    }
}



/*-----------------------------添加更进信息-------------------*/
function submitOk() {
    //提交信息
    var fContent = document.getElementById("fcontent").value;
    if (fContent == "") {
        alert("请输入跟进内容！");
        return;
    }
    if (document.getElementById("cbSeeEstate").checked == true && document.getElementById("txtEstate").value == "") {
        alert("请选择所看房源！");
        return;
    }
    var cid = document.getElementById("customerId").value;
    var fType = document.getElementById("followType").value;
    var fcontent = document.getElementById("fcontent").value;
    var estate = document.getElementById("txtEstate").value;
    var IsSee = document.getElementById("IsSee").value;
    var hid = document.getElementById("housingid").value;

    var fdeptId = document.getElementById("ddeptid").value;
    var fdept = document.getElementById("ddept").value;
    var fUserId = document.getElementById("duserid").value;
    var fUser = document.getElementById("duser").value;

    $("#btnSave").attr("disabled", "true");
    $("#btnSave").attr("value", "正在提交...");

    $.ajax({
        type: 'POST',
        url: '../Handler/HandlerCustomer.ashx',
        data: 'act=CustomerFollowSave1&customerId=' + cid + '&followType=' + fType + '&fcontent=' + fcontent + '&txtEstate=' + estate + '&IsSee=' + IsSee + '&housingid=' + hid + '&fdeptId=' + fdeptId + '&fdept=' + fdept + '&fUserId=' + fUserId + '&fUser=' + fUser,
        timeout: '20000',
        error: function() {
            alert("提交失败！");
            $("#btnSave").removeAttr("disabled");
            $("#btnSave").attr("value", "提交");
        },
        success: function(json) {
            $("#btnSave").removeAttr("disabled");
            $("#btnSave").attr("value", "提交");

            document.getElementById("fcontent").value = "";
            document.getElementById("housingid").value = "0";
            document.getElementById("txtEstate").value = "";
            document.getElementById("IsSee").value = "0";
            document.getElementById("cbSeeEstate").checked = false;
            document.getElementById("followType").value = '电话沟通';


            ShowFollow(1);
        }
    });
}

function IsChecked() {
    var check = document.getElementById("cbSeeEstate");
    if (check.checked == true) {
        document.getElementById("IsSee").value = "1";
        document.getElementById("followType").value = '带看房';
        $("#selectHousing").show();
    } else {
        document.getElementById("IsSee").value = "0";
        document.getElementById("followType").value = '电话沟通';
        $("#selectHousing").hide();

    }
}

function followTypeChange() {
    var followtype = document.getElementById("followType").value;
    var check = document.getElementById("cbSeeEstate");
    if (followtype == "带看房") {
        check.checked = true;
        document.getElementById("IsSee").value = "1";
        $("#selectHousing").show();
    }
    else {
        check.checked = false;
        $("#selectHousing").hide();

    }
}
function ShowDialogHList(url) {
    var path = url;
    var parameter = "";
    var style = "dialogWidth:800px; dialogHeight:380px; status:0;scroll:1; help:0; resizable:1;Scrollbars=yes,Toolbar=no,Location=no,Direction=no,Resizeable=no";
    var returnValue = window.showModalDialog(path, parameter, style);
    if (typeof (returnValue) == "undefined" || returnValue == null)
        return;
    var str = new Array();
    str = returnValue.split('|');
    document.getElementById("housingid").value = str[0];
    document.getElementById("txtEstate").value = str[1];
}

// 同业主客源
function showOtherCustomer() {
    var id = $("#hID").val();

    if (id <= 0)
        return;

    $.ajax({
        type: 'POST',
        url: '../Handler/HandlerCustomer.ashx',
        data: 'act=ShowOtherCustomer&id=' + id,
        timeout: '30000',
        error: function() {
            alert("系统繁忙，请稍候后试！");
        },
        success: function(json) {
        showOtherCustomerCallBack(json);
        }
    });
}

function showOtherCustomerCallBack(response) {
    var json = eval("(" + response + ")");
    var record = json.record;

    if (record == null || record.length == 0) {
        $("#div7").html("该客在本组无其他人所有！此功能中有经理以上才能使用！"); // 清空
        return;
    }

    var arr = [];
    arr.push("<table id='tableList' width='100%' border='0' cellpadding='0' cellspacing='1' bgcolor='#a8c7ce'>");
    arr.push("<tr>");
    arr.push("<td height='25' bgcolor='d3eaef' class='STYLE10'>");
    arr.push("客户名称</td>");
    arr.push("<td height='25' bgcolor='d3eaef' class='STYLE10'>");
    arr.push("交易</td>");
    arr.push("<td height='25' bgcolor='d3eaef' class='STYLE10'>");
    arr.push("状态</td>");
    arr.push("<td height='25' bgcolor='d3eaef' class='STYLE10'>");
    arr.push("城区</td>");
    arr.push("<td height='25' bgcolor='d3eaef' class='STYLE10'>");
    arr.push("片区</td>");
    arr.push("<td height='25' bgcolor='d3eaef' class='STYLE10'>");
    arr.push("所属人</td>");
    arr.push("<td height='25' bgcolor='d3eaef' class='STYLE10'>");
    arr.push("所属部门</td>");
    arr.push("<td height='25' bgcolor='d3eaef' class='STYLE10'>");
    arr.push("所属时间</td>");
    arr.push("</tr>");
    for (var i = 0; i < record.length; i++) {
        if (i % 2 == 0)
            arr.push("<tr id='tr_" + record[i].id + "' bgcolor='#FFFFFF' style='cursor: pointer;'  onclick=\"selectedTr('" + record[i].id + "')\" >");
        else
            arr.push("<tr id='tr_" + record[i].id + "' bgcolor='#EEEDED' style='cursor: pointer;'  onclick=\"selectedTr('" + record[i].id + "')\" >");

        arr.push("<td height='25'  class='STYLE10'>");
        arr.push("<a href='#' onclick=\"openDetail('" + record[i].id + "')\">" + record[i].customer + "</a></td>");
        arr.push("<td  height='25' class='STYLE6'>");

        arr.push("" + record[i].tradetype + "");
        arr.push("</td>");

        arr.push("<td  height='25' class='STYLE6'>");
        if (record[i].isself == 0)
            arr.push("私客");
        else if (record[i].status == 1)
            arr.push("公客");
        arr.push("<td height='25'  class='STYLE10'>");
        arr.push("" + record[i].dname + "</td>");
        arr.push("<td height='25'  class='STYLE10'>");
        arr.push("" + record[i].area + "</td>");
        arr.push("<td height='25'  class='STYLE10'>");
        arr.push("" + record[i].owner + "</td>");
        arr.push("<td height='25'  class='STYLE10'>");
        arr.push("" + record[i].ownerdept + "</td>");
        arr.push("<td height='25'  class='STYLE10'>");
        arr.push("" + record[i].ownerdate + "</td>");
    }
    arr.push("</table>");

    $("#div7").html(""); // 清空
    $("#div7").html(arr.join(''));
}

// 选中某行
function selectedTr(id) {
    $("tr").removeClass("selected");
    $("#tr_" + id).addClass("selected");

    return false;
}

// 查看详情
function openDetail(id) {
    $("tr").removeClass("selected");
    $("#tr_" + id).addClass("selected");


    var url = "../CM/CustomerDetail.aspx?id=" + id;
    var style = "left = 50, screenX = 0, top = 100, screenY = 0, resizable = yes, scrollbars = yes,width=750px, height=450px,Toolbar=no,Location=no,Direction=no,Resizeable=no";

    window.open(url, '', style);


}
